/*
 * XML.java
 *
 * Created on 14 de Julho de 2006, 11:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package negocio;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Rui Cadete & Associados
 * @author Gustavo F. Carvalho
 */
public class XMLutil {
    
    /** Creates a new instance of XML */
    public XMLutil() {
    }
    
    public static void gerarXML (Object conf, String tipo) throws IOException{
        
        XStream xstream = new XStream(new DomDriver());
	ObjectOutputStream obj = xstream.createObjectOutputStream(new FileWriter(".\\"+tipo+".xml"),"config."+tipo);
        obj.writeObject(conf);		
        obj.close();
        String temp = LeitorBuffer.ler(new File(".\\"+tipo+".xml"));
        EscritorBuffer.escreve(new File(".\\"+tipo+".xml"),"<?xml version='1.0' encoding='UTF-8'?>\r\n"+temp);
    }
    
    public static Object gerarObj (String tipo) throws ClassNotFoundException, IOException{
        
        XStream xstream = new XStream(new DomDriver()); 
        ObjectInputStream obj = xstream.createObjectInputStream(new FileReader(".\\"+tipo+".xml"));
        Object config = obj.readObject();
        obj.close();
   
    return config;    
    }
}
